<?php
class DBSQL
{
	private $CONN = "";

	public function __construct()
	{
		try{
			$conn = mysql_connect(ServerName,UserName,PassWord);
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		try{
			mysql_query("SET NAMES utf8");
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		try{
			mysql_select_db(DBName,$conn);
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		$this->CONN = $conn;
	}

	public function select($sql = "")
	{
		if(empty($sql)) return false;
		if(empty($this->CONN)) return false;
		try{
			$results = mysql_query($sql,$this->CONN);
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		if((!$results) || (empty($results))){
			@mysql_free_result($results);
			return false;
		}

		$count = 0;
		$data = array();

		while($row = @mysql_fetch_array($results)){
			$data[$count]= $row;
			$count++;
		}

		@mysql_free_result($results);

		return $data;
	}

	public function getInfo($id,$name)
	{
		$sql = "SELECT * FROM ".$name." WHERE F_ID = $id";
		$r = $this->select($sql);
		return $r[0];
	}

	public function insert($sql = "")
	{
		if(empty($sql)) return 0;
		if(empty($this->CONN)) return 0;
		try{
			$results = mysql_query($sql,$this->CONN);
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		if(!$results)
			return 0;
		else
			return @mysql_insert_id($this->CONN);
	}

	public function insertData($name,$data)
	{
		$field = implode(",",array_keys($data));
		$i = 0;
		foreach($data as $key => $val){
			$value .= "'".$val."'";
			if($i<count($data)-1)
			$value .= ",";
			$i++;
		}
		echo $field;
		echo $value;
		$sql = "INSERT INTO " .$name. "(" .$field. ") VALUES (" .$value. ")";
		return $this->insert($sql);
	}

	public function update($sql = "")
	{
		if(empty($sql)) return 0;
		if(empty($this->CONN)) return 0;
		try{
			$result = mysql_query($sql,$this->CONN);
		}catch(exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		return $result;
	}

	/**
	* 功能：更新指定表指定ID的调查表记录
	* 参数：$name 表名称,$id 表ID,$data 数组(格式：$data['字段名'] = 值)
	* 返回：TRUE OR FALSE
	*/
	public function updateData($name,$id,$data){
		$col = array();
		foreach ($data as $key => $value)
		{
			$col[] = $key . "='" . $value . "'";
		}
		$sql = "UPDATE " . $name . " SET " . implode(',',$col) . " WHERE F_ID = $id";
		return $this->update($sql);
	}
    /**
	 * 功能：数据删除函数
	 * 参数：$sql SQL语句
	 * 返回：TRUE OR FALSE
	 */
	public function delete($sql = ""){
		if(empty($sql)) return false;					//如果SQL语句为空则返回FALSE
		if(empty($this->CONN)) return false;			//如果连接为空则返回FALSE
		try{
			$result = mysql_query($sql,$this->CONN);
		}catch(Exception $e){
			$msg = $e;
			include(ERRFILE);
		}
		return $result;
	}

	public function begintransaction()
	{
		mysql_query("SET  AUTOCOMMIT=0");				//设置为不自动提交，因为MYSQL默认立即执行
		mysql_query("BEGIN");							//开始事务定义
	}

	public function rollback()
	{
		mysql_query("ROOLBACK");
	}

	public function commit()
	{
		mysql_query("COMMIT");
	}
}
?>